package com.kaixinff.kaixin001.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kaixinff.kaixin001.util.Filter;
import com.kaixinff.kaixin001.util.KXClient;
import com.kaixinff.kaixin001.util.KXUtil;
import com.kaixinff.kaixin001.util.RegStep;
import com.kaixinff.kaixin001.xw.Gift;
import com.kaixinff.name.User;

@SuppressWarnings("serial")
public class RunUrlServlet extends HttpServlet {
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/html; charset=" + KXUtil.CHARSET);
		List<User> users = KXUtil.getUsers(new Filter(){
			@Override
			public boolean access(User user) {
				return user.hasStep(RegStep.ADD_XW);
			}}, resp);
		if (users == null) {
			return;
		}
		
		resp.getWriter().println("<form name='form1' method='post' action='/kx/runurl'>");
		resp.getWriter().println("<input type=text name='url' value='http://www.kaixin001.com/!spiderman/" + "!ajax_dojob.php?c_jid=1&cid=2&tmp=" + Math.random()+ "'>");
		resp.getWriter().println("<input type=submit name='submit' value='run'>");
		resp.getWriter().println("</form>");
	}
	
	

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
//		String url = req.getParameter("url");
//		List<User> users = NameUtil.loadUsers(KXUtil.USERS_FILE);
//		for (User user: users) {
//			if (user.hasStep(RegStep.ADD_XW)) {
//				KXClient client = KXClient.getInstance(user.getId());
//				String html = client.doGet(url, null).getContent();
//				if (html.indexOf("登录") > -1) {
//					System.err.println(user.getId());
//				} else {
//					System.out.println(user.getId());
//				}
//			}
//		}

		new Gift(resp, true).start();
		
		/*List<User> users = NameUtil.loadUsers(KXUtil.USERS_FILE);
		int index = 3;
		int ignore = 2;
		for (User user: users) {
			if (user.hasStep(RegStep.ADD_XW)) {
				KXClient client = KXClient.getInstance(user.getId());
				if (index == 1 && ignore > 0) {
					index ++;
					ignore --;
				}
				int result = gift(client, index);
				if (result == 0) {
					index = index % 7 + 1;
				} else if (result == -1) {
					return;
				}
			}
		}*/
	}
	
	/*private int gift(KXClient client, int id) throws IOException {
		String url = "http://www.kaixin001.com/!spiderman/!ajax_gift.php?wkey=Nangua" + id + "_Col&type=collection&msg=&uid=11317137&flag=freegift&cid=2&tmp=" + Math.random() + "&rt=json";
		String html = client.doGet(url, null).getContent();
		if (html.indexOf("\"flag\":\"suc\"") > -1) {
			System.out.println("赠送：" + id);
			return 1;
		} else if (html.indexOf("\\u4e0d\\u80fd\\u518d\\u6536\\u793c\\u7269\\u4e86") > -1) {
			System.out.println("赠送结束！");
			return -1;
		}
		return 0;
	}*/
	
	/**
	 * 
	 * @throws IOException
	 */
	private void gift2Users() throws IOException {
		KXClient client = KXClient.getInstance(KXUtil.getMainUserEmail());
		Properties p = KXUtil.getUid2Email();
		Map<String, String> m = new HashMap<String, String>();
		for (Object key : p.keySet()) {
			String uid = (String) key;
			m.put(p.getProperty(uid), uid);
		}
		List<User> users = KXUtil.getUsers(new Filter(){
			@Override
			public boolean access(User user) {
				return user.hasStep(RegStep.ADD_XW);
			}}, null);
		String[] items = new String[]{
				"Bicycle_Inv",
				"Kuadouche_Inv",
				"Tanke_Inv",
				"Sirenyt_Inv",
				"Junjian_Inv",
				"Sanlunche_Inv",
				"Shepisb_Inv",
				"Gaoyadq_Inv",
				"Haiguimd_Inv",
				"Zhongjq_Inv"
		};
		int[] nums = new int[]{
			52, 43, 35, 55, 98, 32, 52, 57, 42, 30	
		};
		int i = 0;
		for (User user: users) {
			String uid = m.get(user.getId());
			if (uid != null) {
				int num = 0;
				if (nums[i] == 0) {
					i++;
				}
				if (i >= nums.length) {
					return;
				}
				if (nums[i] < 5) {
					num = nums[i];
					nums[i] = 0;
				}
				else if (nums[i] % 5 > 0) {
					num = 6;
					nums[i] = nums[i] - num;
				} else {
					num = 5;
					nums[i] = nums[i] - num;
				}
				String url = KXUtil.HOST_URL + "/!spiderman/!ajax_gift.php?wkey=" + items[i] + "&type=loot&num=" + num + "&msg=&uid=" + uid + "&flag=sendgift&cid=5&tmp=" + Math.random() + "&rt=json";
				String html = client.doGet(url, null).getContent();
			}
		}
	}
}
